package cn.argentoaskia.crm.service.login.impl;

import cn.argentoaskia.crm.bean.Hr;
import cn.argentoaskia.crm.dao.HrDAO;
import cn.argentoaskia.crm.result.ResponseData;
import cn.argentoaskia.crm.service.login.HrService;
import cn.argentoaskia.crm.utils.Md5Util;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class HrServiceImpl implements HrService {



    @Autowired
    private HrDAO hrDAO;

    @Override
    public boolean login(String username, String password, ResponseData responseData) {
        // 后期这个方法会标记事务注解
        // 1.用户信息
        Hr hr = hrDAO.selectByUsername(username);
        // hrDAO:username、password(MD5值)
        String passwordHash = Md5Util.md5Hash(password, username, 1000);

        // 核心登录业务
        // 登录成功
        if(passwordHash.equals(hr.getPassword())){
            hr.setPhone(null);
            hr.setTelephone(null);
            hr.setAddress(null);
            hr.setEnabled(null);
            hr.setPassword("");
            // UUID
            // 后期UUID放在Redis
            // 直接回存数据库
            String uuid = UUID.randomUUID().toString();
            hr.setRemark(uuid);
            // 2022/9/15 updateRemark回传数据库修改Remark字段
            Hr hr1 = new Hr();
            hr1.setId(hr.getId());
            hr1.setRemark(hr.getRemark());
            hrDAO.update(hr1);

            Map<String, Hr> returnData = new HashMap<>();
            returnData.put("userinfo", hr);
            // 参考型变量
            responseData.setData(returnData);
            responseData.setCount(null);
            return true;
        }
        else{
            Map<String, String> returnData = new HashMap<>();
            returnData.put("info", "用户名和密码错误");
            responseData.setData(returnData);
            responseData.setCount(null);
            return false;
        }

    }
}
